#include <stdio.h>
#include <stdlib.h>

struct node {
    int value;
    struct node *next;
};
struct node *add_to_list(struct node *list, int n);
struct node *read_numbers(struct node *first, int n);
int search_list(struct node *first);
struct node *delete_from_list(struct node *list);

int main()
{
    struct node *first = NULL;
    int n = 0;
    
    first = read_numbers(first, n);
    printf("%d\n", search_list(first));
    first = delete_from_list(first);
    printf("%d\n", search_list(first));

    return 0;
}

struct node *read_numbers(struct node *first, int n)
{
    printf("Enter a series of integers : ");
    for (;;)
    {
        scanf("%d", &n);
        if (n == 0)
            return first;
        first = add_to_list(first, n);
    }
}

struct node *add_to_list(struct node *list, int n)
{
    struct node *new_node;

    new_node = malloc(sizeof(struct node));
    if (new_node == NULL)
    {
        printf("Error: malloc failed in add_to list\n");
        exit(EXIT_FAILURE);
    }
    new_node->value = n;
    new_node->next = list;

    return new_node;
}

int search_list(struct node *first)
{
    int location = 1, number;

    printf("Enter the number you look for : ");
    scanf("%d", &number);

    for (struct node *p = first; p != NULL; p = p->next)
    {
        if (p->value == number)
            break;
        location++;
    }

    return location;
}

struct node *delete_from_list(struct node *list)
{
    struct node *cur, *prev;
    int n = 0;
    printf("Enter the number to delete : ");
    scanf("%d", &n);

    for (cur = list, prev = NULL;
         (cur != NULL) & (cur->value != n);
         prev = cur, cur = cur->next)
        ;

    if (cur == NULL)
        return list;
    if (prev == NULL)
        list = list->next;
    else
        prev->next = cur->next;

    free(cur);
    return list;
}